package ligne2vie.topos.client.panels;

import ligne2vie.topos.client.gwt.proxy.MyDecoractedStackPanel;

import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * The class {@link ActionProcessPanel} proposes a convenience base to have panel displaying each step of an action
 * process, each step being displayed into one of the stacks.
 * 
 * @author thuguerre
 * @since 1.0.0
 */
public class ActionProcessPanel extends DecoratorPanel {

	/**
	 * {@link ActionProcessPanel}'s singleton instance.
	 */
	private static ActionProcessPanel instance = null;

	/**
	 * {@link ActionProcessPanel}'s title.
	 */
	private Label title = new Label("");

	/**
	 * The decorated stack used to display all actions to perform.
	 */
	private MyDecoractedStackPanel stack = new MyDecoractedStackPanel();

	/**
	 * Default private constructor making this class as a singleton.
	 */
	private ActionProcessPanel() {

		VerticalPanel insider = new VerticalPanel();
		insider.setStylePrimaryName("actionInsiderVP");
		
		insider.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		insider.add(title);
		insider.add(stack);

		this.add(insider);
	}

	/**
	 * Allows adding a new widget to the stack in order to complete it.
	 * 
	 * @param widget
	 *            Widget to add.
	 * @param title
	 *            Title under which adding the widget.
	 */
	public void addStackWidget(Widget widget, String title) {
		this.stack.add(widget, title);
	}

	/**
	 * The method {@link #enableStep(int)} allows enabling a new step in the current process, hiding and unabling all
	 * others.
	 * 
	 * @param index
	 *            Index of the step to enable.
	 */
	public void enableStep(int index) {

		stack.showStack(index);

		for (int i = 0; i < stack.getWidgetCount(); i++) {
			if (i != index) {
				stack.getWidget(index).setVisible(false);
			}
		}

		stack.getWidget(index).setVisible(true);
	}

	/**
	 * @return {@link ActionProcessPanel}'s singleton instance.
	 */
	public static ActionProcessPanel getInstance() {

		if (instance == null) {
			instance = new ActionProcessPanel();
		}

		return instance;
	}

	/**
	 * The method {@link #init()} allows initializing this panel between each action.
	 * 
	 * @param title
	 */
	public void init(String title) {

		this.setTitle(title);

		while (stack.getWidgetCount() > 0) {
			stack.remove(0);
		}
	}

	/**
	 * Allows setting the title on the panel.
	 * 
	 * @param title
	 *            Title to set.
	 */
	public void setTitle(String title) {
		this.title.setText(title);
		this.title.setStylePrimaryName("actionTitle");
	}

	/**
	 * The method {@link #startAction()} configures the current action process widget in order to start a new action.
	 */
	public void startAction() {

		ControlPanel.getInstance().setVisible(false);
		this.setVisible(true);
		this.enableStep(0);
	}

	/**
	 * The method {@link #stopAction()} configures the current action process widget in order to stop the current
	 * action.
	 */
	public void stopAction() {

		this.setVisible(false);
		ControlPanel.getInstance().setVisible(true);

		this.init("");
	}
}
